﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using canthovieclam.DAL;
using canthovieclam.DAL.Repositories;
using canthovieclam.BL.Models;
using canthovieclam.BL.DataContact;
using canthovieclam.Core.Utilities;

namespace canthovieclam.BL
{
    public class JobCatelogyImpl
    {
        /// <summary>
        /// Get hierarchy job categories
        /// </summary>
        /// <returns></returns>
        public List<JobCategoryInfo> GetHierarchyJobCategoryList()
        {
            var jobCategoryRepository = new JobCategoryRepository();
            var jobCategoryDescriptionRepository = new JobCategoryDescriptionRepository();
            List<JobCategoryModel> listJobCategoryModel = (from x in jobCategoryRepository.GetAllJobCategory()
                                                           join y in jobCategoryDescriptionRepository.GetAllJobCategoryDescription()
                                                           on x.JobCategoryId equals y.JobCategoryId
                                                           where y.LanguageId == 0
                                                           select new JobCategoryModel(x) { JobCategoryName = y.JobCategoryName }).ToList();
            List<JobCategoryInfo> listJobCategoryInfo = new List<JobCategoryInfo>();
            foreach (JobCategoryModel jobCategoryModel in listJobCategoryModel)
            {
                JobCategoryInfo info = new JobCategoryInfo();
                if (jobCategoryModel.ParentId == null)
                {
                    info.ParentCategory = jobCategoryModel;
                    info.ChildCategories = new List<JobCategoryModel>();
                    foreach (JobCategoryModel childJobCategoryModel in listJobCategoryModel)
                    {
                        if (childJobCategoryModel.ParentId == jobCategoryModel.JobCategoryId)
                        {
                            info.ChildCategories.Add(childJobCategoryModel);
                        }
                    }
                    listJobCategoryInfo.Add(info);
                }
            }
            return listJobCategoryInfo;
        }

        /// <summary>
        /// Get the job categories
        /// </summary>
        /// <returns></returns>
        public List<JobCategoryModel> GetJobCategoryList()
        {
            var jobCategoryRepository = new JobCategoryRepository();
            var jobCategoryDescriptionRepository = new JobCategoryDescriptionRepository();
            List<JobCategoryModel> listJobCategoryModel = (from x in jobCategoryRepository.GetAllJobCategory()
                                                           join y in jobCategoryDescriptionRepository.GetAllJobCategoryDescription()
                                                           on x.JobCategoryId equals y.JobCategoryId
                                                           select new JobCategoryModel(x) { JobCategoryName = y.JobCategoryName }).ToList();
            return listJobCategoryModel;
        }

        public void CreateSampleData()
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var jobCategoryRepository = new JobCategoryRepository(unitOfWork);
                var jobCategoryDescriptionRepository = new JobCategoryDescriptionRepository(unitOfWork);
                var jobCategoryId = GuidUtils.NewSeqGuid();
                var jobParentId = jobCategoryId;
                jobCategoryRepository.AddJobCategory(new JobCategory { JobCategoryId = jobCategoryId });
                jobCategoryDescriptionRepository.AddJobCategoryDescription(new JobCategoryDescription
                {
                    JobCategoryId = jobCategoryId,
                    JobCategoryName = "Công nghệ thông tin/ Máy tính"
                });

                jobCategoryId = GuidUtils.NewSeqGuid();
                jobCategoryRepository.AddJobCategory(new JobCategory { JobCategoryId = jobCategoryId, ParentId = jobParentId });
                jobCategoryDescriptionRepository.AddJobCategoryDescription(new JobCategoryDescription
                {
                    JobCategoryId = jobCategoryId,
                    JobCategoryName = "CNTT - Phần mềm"
                });

                jobCategoryId = GuidUtils.NewSeqGuid();
                jobCategoryRepository.AddJobCategory(new JobCategory { JobCategoryId = jobCategoryId, ParentId = jobParentId });
                jobCategoryDescriptionRepository.AddJobCategoryDescription(new JobCategoryDescription
                {
                    JobCategoryId = jobCategoryId,
                    JobCategoryName = "CNTT - Phần cứng/ Mạng"
                });

                unitOfWork.Commit();
            }
        }
    }
}
